﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Expression.Encoder;

namespace EncoderFileWatcherApp
{
    class FileConverter
    {
        public static bool IsBusy;
        protected static Job _job;
        public delegate void ProgressEventHandler(object sender, EncodeProgressEventArgs e);
        public static event ProgressEventHandler ProgressEvent;
        public static string LastError = "";

        public static string GetTargetExtenstion(string profileName)
        {
            throw new NotImplementedException("Not yet implemented.  This method would return the file extension of the files created by the profile.");
          //  Microsoft.Expression.Encoder.OutputFile f = new OutputFile();
        }

        public static string GetCurrentTargetFileName()
        {
            string name = "";
            if (_job != null)
            {
                if (_job.MediaItems.Count > 0)
                {
                    name = _job.MediaItems[0].ActualOutputFileFullPath;
                }
            }
            return name;
        }

        public static bool ConvertFile(string fileName, string profileName, string outputDirectory)
        {
            bool bSuccess = false;
            if (!IsBusy)
            {
                 try
                {
                    //was not busy
                    IsBusy = true;
                    if (_job != null)
                    {
                        //any cleanup required?
                    }
                    //MediaItem mediaItem = new MediaItem(@"M:\Video\RawFootage\2004-06-27 07.54.07.avi");
                    MediaItem mediaItem = new MediaItem(fileName);

                    // Create a job and the media item for the video we wish
                    // to encode.
                    _job = new Job();
                    _job.MediaItems.Add(mediaItem);

                    // Set up the progress callback function
                    _job.EncodeProgress
                        += new EventHandler<EncodeProgressEventArgs>(OnProgress);

                    // Set the output directory and encode.
                    _job.OutputDirectory = outputDirectory;
                    _job.CreateSubfolder = false;
                    //_job.ApplyPreset(@"C:\Users\sdf\Documents\Expression\Expression Encoder\JobPresets\HomeVideoPreset_720x480_WMV_QualityVBR_90.xml");
                    _job.ApplyPreset(profileName);

                    _job.Encode();
                    bSuccess = true;
                }
                catch (Exception ex)
                {
                    LastError = ex.ToString();
                    bSuccess = false;
                }
                finally
                {
                    //always reset our flag
                    IsBusy = false;
                }
            }
            else
            {
                //was busy
                bSuccess = false;
            }
            return bSuccess;
        }

        static void OnProgress(object sender, EncodeProgressEventArgs e)
        {
            if (ProgressEvent != null)
            {
                //Console.WriteLine(e.Progress);
                ProgressEvent(sender, e);
            }
        }

        /// <summary>
        /// Stops the job, and returns the name of the file being stopped.
        /// </summary>
        /// <returns></returns>
        public static string Stop()
        {
            string fileName = "";
            if (_job != null)
            {
                if (_job.MediaItems.Count > 0)
                {
                    fileName = _job.MediaItems[0].ActualOutputFileFullPath;
                }
                _job.CancelEncode();
                _job.Dispose();
            }
            return fileName;
        }

        
    }
}
